<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .el-table .warning-row {
            background: oldlace;
        }

        .el-table .success-row {
            background: #f0f9eb;
        }
    </style>

</head>
<body>
<div id="app">


    <!--按钮-->

    <el-row>
        <el-button type="primary" plain @click="dialogVisible = true">新增</el-button>
    </el-row>

    <!--添加数据对话框表单-->
    <el-dialog
            title="编辑用户"
            :visible.sync="dialogVisible"
            width="30%"
            >

        <el-form ref="form" :model="user" label-width="80px">
            <el-form-item label="用户名称">
                <el-input v-model="user.username"></el-input>
            </el-form-item>

            <el-form-item label="邮箱">
                <el-input v-model="user.email"></el-input>
            </el-form-item>

            <el-form-item label="年龄">
                <el-input v-model="user.age"></el-input>
            </el-form-item>


            <el-form-item>
                <el-button type="primary" @click="addBrand">提交</el-button>
                <el-button @click="dialogVisible = false;user = {}">取消</el-button>
                <el-button @click="user = {}">重置</el-button>
            </el-form-item>
        </el-form>

    </el-dialog>


    <!--表格-->
    <template>
        <el-table
                :data="tableData"
                style="width: 100%"
                :row-class-name="tableRowClassName"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    type="index"
                    width="50">
            </el-table-column>

            <el-table-column
                    prop="username"
                    label="用户名称"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="email"
                    label="邮箱"
                    align="center"
            >
            </el-table-column>
            <el-table-column
                    prop="age"
                    align="center"
                    label="年龄">
            </el-table-column>


            <el-table-column
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-row>
                        <el-button type="primary" @click="toEdit(scope.row)">修改</el-button>
                        <el-button type="danger" @click="delById(scope.row)">删除</el-button>
                    </el-row>
                </template>
            </el-table-column>

        </el-table>
    </template>

    <!--分页工具条-->
    <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[5, 10, 15, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="totalCount">
    </el-pagination>

</div>

<script src="../js/axios-0.18.0.js"></script>
<script src="../js/vue.js"></script>
<script src="../element-ui/lib/index.js"></script>
<link rel="stylesheet" href="../element-ui/lib/theme-chalk/index.css">

<script>
    new Vue({
        el: "#app",
        mounted(){
            this.selectAll();
        },
        methods: {
            //修改，弹出修改框
            toEdit(val){//val当前行的数据！！
                this.dialogVisible = true;//弹框
                //TODO 回显数据！！ val.status 数值   ===> 转成字符串类型
                val.status = val.status+"";
                this.user= JSON.parse(JSON.stringify(val));
                console.log(val);
            },

            //删除
            delById(val){//当前行的数据
                this.$confirm('您确定要删除该品牌吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    //确定
                    axios({
                        url:'/users/'+val.id,
                        method:'delete'
                    }).then((res)=>{
                        if(res.data.code == 20021){
                            // 重新查询数据
                            this.selectAll();
                            // 弹出消息提示
                            this.$message({
                                message: '恭喜你，删除成功！',
                                type: 'success'
                            });
                        }
                    });
                }).catch(() => {
                    //取消 啥都不做！！

                });
            },

            //查询
            selectAll(){
                axios.get("http://localhost/users?currentPage="+this.currentPage+"&pageSize="+this.pageSize)
               .then(res=>{
                    this.tableData = res.data.data.rows;
                    this.tableData = res.data.data;
                    this.totalCount=res.data.data.totalCount;
                });
            },

            tableRowClassName({row, rowIndex}) {
                if (rowIndex === 1) {
                    return 'warning-row';
                } else if (rowIndex === 3) {
                    return 'success-row';
                }
                return '';
            },
            // 查询方法
            onSubmit() {
                console.log(this.user);
                this.selectAll();
            },
            // 添加数据
            addBrand(){
                console.log(this.user);
                this.methodStyle = this.user.id==''?'post':'put';
                alert(this.methodStyle)
                axios({
                    url:"http://localhost/users",
                    method:this.methodStyle,
                    data: this.user
                }).then((res)=>{
                    console.log(res.data.code)
                    this.user={};
                    this.user.id='';
                    if(res.data.code == 20011){
                        //关闭窗口
                        this.dialogVisible = false
                        // 重新查询数据
                        this.selectAll();
                        // 弹出消息提示
                        this.$message({
                            message: '恭喜你，添加操作成功！',
                            type: 'success'
                        });
                    }
                    if(res.data.code == 20031){
                        //关闭窗口
                        this.dialogVisible = false
                        // 重新查询数据
                        this.selectAll();
                        // 弹出消息提示
                        this.$message({
                            message: '恭喜你，更新操作成功！',
                            type: 'success'
                        });
                    }

                });
            },

            //分页
            handleSizeChange(val) {//pageSize
                console.log(`每页 ${val} 条`);
                this.pageSize=val;
                this.selectAll();
            },
            handleCurrentChange(val) {//currentPage
                console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.selectAll();
            }

        },
        data() {
            return {
                methodStyle:'',
                pageSize:5,
                totalCount:0,
                selectedIds:[],
                // 当前页码
                currentPage: 1,
                // 添加数据对话框是否展示的标记
                dialogVisible: false,

                //用户模型数据
                user: {
                    username: '',
                    id:"",
                    age:"",
                    email:""
                },
                // 表格数据
                tableData: []
            }
        }
    })

</script>

</body>
</html>
